
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class searchengine
{
	private String engineurl;
	
	public searchengine(String host)
	{
		engineurl = host;
	}
	public void search(String query, int page)
	{
		if(this.engineurl.contains("google"))
			search_google(query, page);
		else if(this.engineurl.contains("baidu"))
			search_baidu(query, page);
		else if(this.engineurl.contains("yahoo"))
			search_yahoo(query, page);
		else
			return;
	}
	private void search_baidu(String query, int page)
	{
		System.out.println("+++On baidu search+++");
		try
		{
			String ret = "";
			for(int i=0; i<page*10; i+=10)
			{
				System.out.println("+++Page "+ (i/10+1) +" +++");
				URL url = new URL(engineurl + "/s?wd=" + query + "&pn=" + i);
				String cookie = "";
				URLConnection connection = url.openConnection();
				connection.setRequestProperty("Cookie", cookie);
				connection.setRequestProperty("Cache-Control", "no-cache");
				connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 8.0)");
				connection.setConnectTimeout(10000);    
				connection.setReadTimeout(10000);
				
				InputStream in = connection.getInputStream();
				byte[] buffer = new byte[1024];
				String encoding = "GB2312";
				
				BufferedReader br = new BufferedReader(new InputStreamReader(in, encoding));
				String header = new String(buffer, encoding);
				StringBuffer sb = new StringBuffer(header);
				char[] charBuf = new char[2048];
				int len = br.read(charBuf);
				while (len != -1) {
					sb.append(charBuf, 0, len);
					len = br.read(charBuf);
				}
				br.close();
				ret = sb.toString();
				if(!ret.trim().startsWith("<"))
					ret = "<" + ret.trim();
				System.out.println("+++Get return+++");
				System.out.println("+++Received: "+ ret.length() +" bytes+++");
				htmlparser parser = new htmlparser(ret);
				parser.getCenter(1);
			}
		}
		catch(Exception e)
		{
			System.out.println("+++Error occur+++");
			System.out.print(e.toString());
			return;
		}
	}
	private void search_yahoo(String query, int page)
	{
		System.out.println("+++On yahoo search+++");
		try
		{
			String ret = "";
			for(int i=0; i<page; i++)
			{
				System.out.println("+++Page "+ (i/10+1) +" +++");
				URL url = new URL(engineurl + "/s?q=" + query + "&page=" + i);
				String cookie = "";
				URLConnection connection = url.openConnection();
				connection.setRequestProperty("Cookie", cookie);
				connection.setRequestProperty("Cache-Control", "no-cache");
				connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 8.0)");
				connection.setConnectTimeout(10000);    
				connection.setReadTimeout(10000);
				
				InputStream in = connection.getInputStream();
				byte[] buffer = new byte[1024];
				String encoding = "utf-8";
				
				BufferedReader br = new BufferedReader(new InputStreamReader(in, encoding));
				String header = new String(buffer, encoding);
				StringBuffer sb = new StringBuffer(header);
				char[] charBuf = new char[2048];
				int len = br.read(charBuf);
				while (len != -1) {
					sb.append(charBuf, 0, len);
					len = br.read(charBuf);
				}
				br.close();
				ret = sb.toString();
				if(!ret.trim().startsWith("<"))
					ret = "<" + ret.trim();
				System.out.println("+++Get return+++");
				System.out.println("+++Received: "+ ret.length() +" bytes+++");
				htmlparser parser = new htmlparser(ret);
				parser.getCenter(2);
			}
		}
		catch(Exception e)
		{
			System.out.println("+++Error occur+++");
			System.out.print(e.toString());
			return;
		}
	}
	private void search_google(String query, int page)
	{
		System.out.println("+++On google search+++");
		try
		{
			String ret = "";
			for(int i=0; i<page*10; i+=10)
			{
				System.out.println("+++Page "+ (i/10+1) +" +++");
				URL url = new URL(engineurl + "/search?q=" + query + "&start=" + i);
				String cookie = "";
				URLConnection connection = url.openConnection();
				connection.setRequestProperty("Cookie", cookie);
				connection.setRequestProperty("Cache-Control", "no-cache");
				connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 8.0)");
				connection.setConnectTimeout(100000);    
				connection.setReadTimeout(100000);
				
				InputStream in = connection.getInputStream();
				byte[] buffer = new byte[1024];
				String encoding = "utf-8";
				
				BufferedReader br = new BufferedReader(new InputStreamReader(in, encoding));
				String header = new String(buffer, encoding);
				StringBuffer sb = new StringBuffer(header);
				char[] charBuf = new char[2048];
				int len = br.read(charBuf);
				while (len != -1) {
					sb.append(charBuf, 0, len);
					len = br.read(charBuf);
				}
				br.close();
				ret = sb.toString();
				if(!ret.trim().startsWith("<"))
					ret = "<" + ret.trim();
				System.out.println("+++Get return+++");
				System.out.println("+++Received: "+ ret.length() +" bytes+++");
				htmlparser parser = new htmlparser(ret);
				parser.getCenter(0);
			}
		}
		catch(Exception e)
		{
			System.out.println("+++Error occur+++");
			System.out.print(e.toString());
			return;
		}
	}
};